package com.ysq.www.api.front.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ysq.www.api.front.entity.Activity;
import com.ysq.www.api.front.entity.Information;
import com.ysq.www.api.front.service.IInformationService;
import com.ysq.www.common.annotation.AuthToken;
import com.ysq.www.common.result.ResultMsg;
import com.ysq.www.common.utils.Comm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 资讯表 前端控制器
 * </p>
 *
 * @author dql
 * @since 2020-08-07
 */
@Slf4j
@RestController
@RequestMapping("/front/information")
public class InformationController {

    @Autowired
    private IInformationService informationService;

    /**
     * 首页分页查询最新资讯
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param userId 用户id
     * @param asId 协会id
     * @return ResultMsg
     */
    @GetMapping("/search")
    @AuthToken
    public ResultMsg searchByPage(@RequestParam(defaultValue = "5") int pageSize,
                                  @RequestParam(defaultValue = "1") int pageNumber,
                                  Integer userId,Integer asId){
        IPage<Information> iPage = informationService.searchByPage(pageNumber,pageSize,userId,asId);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(iPage);
    }

    /**
     * 查询资讯详情
     * @param imId 资讯id
     * @param userId 用户id
     * @return ResultMsg
     */
    @GetMapping("/detail")
    @AuthToken
    public ResultMsg selectDetailById(@RequestParam(required = true) Integer imId,
                                      Integer userId){
        Information im = informationService.selectDetailById(imId,userId);
        return ResultMsg.SUCCESS.setNewMsg("查询详情成功").setNewData(im);
    }


    /**
     * 提交资讯
     * @param im 资讯实体对象
     * @return ResultMsg
     */
    @PostMapping("/add")
    @AuthToken
    public ResultMsg addInformation(@RequestBody Information im){
        ResultMsg rm = informationService.addInformation(im);
        return rm;
    }

    /**
     * 待办分页查询我发起的资讯
     * @param userId 发起人id
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param time 日期
     * @return ResultMsg
     */
    @GetMapping("/sponsor")
    @AuthToken
    public ResultMsg selectSponsorByPage(Integer userId,Integer pageNumber,Integer pageSize,String time){
        //if (StringUtils.isEmpty(time)) time = Comm.getSysDate(7);
        IPage<Information> iPage = informationService.selectSponsorByPage(userId,pageNumber,pageSize,time);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(iPage);
    }

    /**
     * 分页查询待处理/已处理资讯
     * @param userId 审批人id
     * @param type 类型：0未处理，1已处理
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param time 日期
     * @return ResultMsg
     */
    @GetMapping("/pending")
    @AuthToken
    public ResultMsg selectPendingByPage(Integer userId,Integer type,
                                         Integer pageNumber,Integer pageSize,String time){
        //if (StringUtils.isEmpty(time)) time = Comm.getSysDate(7);
        IPage<Information> iPage = informationService.selectPendingByPage(userId,type,pageNumber,pageSize,time);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(iPage);
    }

    /**
     * 分页查询抄送至我的资讯
     * @param userId 用户id
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param time 日期
     * @return ResultMsg
     */
    @GetMapping("/copyTo")
    @AuthToken
    public ResultMsg selectCopyToMeByPage(Integer userId,Integer pageNumber,Integer pageSize,String time){
        //if (StringUtils.isEmpty(time)) time = Comm.getSysDate(7);
        IPage<Information> aPage = informationService.selectCopyToMeByPage(userId,pageNumber,pageSize,time);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(aPage);
    }


    /**
     * 处理审批资讯
     * @param imId 资讯ID
     * @param result 处理结果：1通过，2驳回
     * @return ResultMsg
     */
    @GetMapping("/check")
    @AuthToken
    public ResultMsg checkInformationById(@RequestParam(required = true) Integer imId,
                                          @RequestParam(required = true) Integer result){
        ResultMsg rm = informationService.checkInformationById(imId,result);
        return rm;
    }


    /**
     * 撤销提出但还未处理的资讯
     * @param imId 资讯id
     * @return ResultMsg
     */
    @GetMapping("/repeal")
    @AuthToken
    public ResultMsg repealInformationById(@RequestParam(required = true) Integer imId){
        ResultMsg rm = informationService.repealInformationById(imId);
        return rm;
    }




}
